<template>
  <div class="permission-container">
    <!-- 提供了一些默认的内边距 padding -->
    <div class="app-container">
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPer(1,'0')">添加权限</el-button>
        </div>
        <el-table :data="list" row-key="id" border>
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="code" label="标识" />
          <el-table-column prop="description" label="描述" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type===1" size="small" type="text" @click="addPer(2,row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="editPer(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="delPer(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增/编辑的弹层 -->
      <el-dialog :visible="showDialog" :title="showTitle" @close="handleClose">
        <!-- 表单内容 -->
        <el-form v-model="formData" v-loading="loading" label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="handleClose">取消</el-button>
            <el-button type="primary" @click="clickSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { reqAddPermission, reqDelPermission, reqGetPermissionDetail, reqGetPermissionList, reqUpdatePermission } from '@/api/permission'
import { transListToTree } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 权限名称
        code: '', // 权限标识,用于将来判断
        description: '', // 权限描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      },
      loading: false
    }
  },
  computed: {
    showTitle() {
      return this.formData.pid ? '编辑权限' : '添加权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const { data } = await reqGetPermissionList()
      // console.log(data)
      this.list = transListToTree(data, '0')
      // console.log(this.list)
    },
    addPer(type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    delPer(id) {
      this.$confirm('确认删除？', '提示', { type: 'warning' }).then(async() => {
        const res = await reqDelPermission(id)
        this.$message.success(res.message)
        this.getPermissionList()
      })
    },
    async editPer(id) {
      this.loading = true
      this.showDialog = true
      const { data } = await reqGetPermissionDetail(id)
      console.log(data)
      this.formData = data
      this.loading = false
    },
    handleClose() {
      this.showDialog = false
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 权限名称
        code: '', // 权限标识,用于将来判断
        description: '', // 权限描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    },
    async clickSubmit() {
      if (!this.formData.pid) {
        const res = await reqAddPermission(this.formData)
        console.log(res)
        this.$message.success(res.message)
      } else {
        const res = await reqUpdatePermission(this.formData)
        this.$message.success(res.message)
      }
      this.handleClose()
      this.getPermissionList()
    }
  }
}
</script>
<style scoped></style>
